package com.vogtec.fota.dao;

import com.vogtec.fota.dao.model.UpgradeTask;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wei on 2017/7/3.
 */
public interface UpgradeTaskDao {

    String TABLE = "upgrade_task";

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert(" insert into "
            + TABLE
            + " set "
            + " taskName=#{taskName}, "
            + " firmWareId=#{firmWareId}, "
            + " firmWareName=#{firmWareName}, "
            + " firmWareVersion=#{firmWareVersion}, "
            + " completion='0.0', "
            + " startTime=#{startTime}, "
            + " endTime=#{endTime}, "
            + " productModel=#{productModel}, "
            + " taskDes=#{taskDes}, "
//            + " createTime=#{createTime}"
            + " taskStatus='0'"
    )
    int insert(UpgradeTask upgradeTask);

    /**
     * 根据id查询升级任务
     * @param id
     * @return
     */
    @Select(" select * from " + TABLE + " where id=#{id}")
    UpgradeTask selectByid(@Param("id") Integer id);

    /**
     * 查询已结束的任务
     * @return
     */
    @Select(" select * from " + TABLE + " where taskStatus = 2 or taskStatus = 3 order by createTime desc")
    ArrayList<UpgradeTask> selectFinish();

    /**
     * 根据状态查询升级任务
     * @param taskStatus
     * @return
     */
    @Select(" select * from " + TABLE + " where taskStatus=#{taskStatus} order by createTime desc")
    ArrayList<UpgradeTask> selectByStatus(@Param("taskStatus") String taskStatus);

    /**
     * 根据状态查询升级任务条数
     * @param taskStatus
     * @return
     */
    @Select(" select count(*) from " + TABLE + " where taskStatus=#{taskStatus}")
    int selectCountByStatus(@Param("taskStatus") String taskStatus);

    /**
     * 查询所有升级任务
     * @return
     */
    @Select(" select * from " + TABLE + " order by createTime desc")
    ArrayList<UpgradeTask> selectAll();

    List<UpgradeTask> selectAllDetail();

    /**
     * 查询所有升级任务条数
     * @return
     */
    @Select(" select count(*) from " + TABLE)
    int selectCountAll();

    @Select(" select * from " + TABLE + " where firmWareId=#{firmWareId}")
    List<UpgradeTask> selectByFirmId(@Param("firmWareId") Integer firmWareId);

    @Select(" select count(*) from " + TABLE + " where firmWareId=#{firmWareId}")
    String selectTaskNumByFirmId(@Param("firmWareId") Integer firmWareId);

    @Select(" select startTime from " + TABLE + " where firmWareId=#{firmWareId} order by startTime desc limit 1")
    Timestamp selectStartTimeByFirmId(@Param("firmWareId") Integer firmWareId);

    /**
     * 更新完成状态
     * @param taskStatus
     * @return
     */
    @Update(" update " + TABLE + " set taskStatus=#{taskStatus} where id=#{id}")
    void updateStatus(@Param("taskStatus") int taskStatus,@Param("id") int id);

    /**
     * 更新实际完成时间
     * @param compTime
     * @param id
     */
    @Update(" update " + TABLE + " set compTime=#{compTime} where id=#{id}")
    void updateCompTime(@Param("compTime") Timestamp compTime,@Param("id") int id);

    /**
     * 更新实际开始时间
     * @param realStartTime
     * @param id
     */
    @Update(" update " + TABLE + " set realStartTime=#{realStartTime} where id=#{id}")
    void updateStartTime(@Param("realStartTime") Timestamp realStartTime,@Param("id") int id);

    /**
     * 更新完成率
     * @param completion
     * @return
     */
    @Update(" update " + TABLE + " set completion=#{completion} where id=#{id}")
    void updateCompletion(@Param("completion") String completion,@Param("id") int id);

    /**
     * 根据id删除升级任务
     * @param id
     * @return
     */
    @Delete(" delete from " + TABLE + " where id=#{id}")
    int delete(@Param("id") Integer id);


}